﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Web.Song.ViewModels;
using System.Text.RegularExpressions;
using System.Text;
using HelloMinasan.Business;
using HelloMinasan.Framework.Web;
using HelloMinasan.Domain;
using HelloMinasan.Business.Extend;

namespace HelloMinasan.Web.Song.Controllers
{
    public class QAController : ApplicationController//BaseController
    {
        //private IRepository._QAIRepository QARepository;
        //public QAController()
        //    : this(new QARepository())
        //{
        //}

        //public QAController(IRepository._QAIRepository QARepository)
        //{
        //    // TODO: Complete member initialization
        //    this.QARepository = QARepository;
        //}

        private IUserService userService;
        private ISongService songService;

        public QAController(ISongService songService, IUserService userService)
        {
            this.songService = songService;
            this.userService = userService;
        }

        //trang trính
        public ActionResult Index(string submitButton)
        {
            var model = songService.QA_TopView();
            switch (submitButton)
            {
                case "Vocabulary":
                    // delegate 
                    return (QA_Catagory("Vocabulary"));
                case "Grammar":
                    // 
                    return (QA_Catagory("Grammar"));
                case "Request document":
                    // 
                    return (QA_Catagory("Request document"));
                case "Others":
                    // 
                    return (QA_Catagory("Others"));
                default:
                    // 
                    return View(model);
            }
        }
        //load theo phân loại
        public ActionResult QA_Catagory(string para)
        {
            var model = songService.QA_Catagory(para);
            return View(model);
        }
        //load theo danh sách top new
        public ActionResult ShowQaTopNew()
        {
            var model = songService.QA_TopNew();
            return PartialView(model);
        }
        //load theo danh sach câu hỏi được trả lời nhiều nhất
        public ActionResult ShowQaTopAnsert()
        {
            var model = songService.QA_TopAnsert();

            return PartialView(model);
        }
        //top 6, hay mục liên quan
        public ActionResult QA_TopNew5()
        {
            var model = songService.QA_TopNew5();

            return PartialView(model);
        }
        //
        // GET: /QA/Details/5
        [HttpGet]
        public ActionResult Details(int ID)
        {
            ViewData["QAID"] = ID;
            if (CurrentUser != null)
            {
                ViewData["LogOnOK"] = "TRUE";
            }
            else
            {
                ViewData["LogOnOK"] = "FALSE";
            }
            QA model = songService.FindQaByID(ID);
            Thanks(model);
            model.SumView = model.SumView + 1;
            songService.UpdateView(model);
            return View(model);
        }

        [HttpPost]
        public ActionResult Details(int ID, string QAID)
        {
            QA model = songService.FindQaByID(ID);
            Thanks(model);
            return View(model);

        }

        public bool Thanks(QA model)
        {
            //kiểm cha người dùng đã thanks chưa
            if (model.Thanks != null)
            {
                string dk = "[^>]+";
                Regex r = new Regex(dk);
                MatchCollection m = r.Matches(model.Thanks);
                int[] mangUserID = new int[m.Count];
                string[] mangUserName = new string[m.Count];
                //lấy danh sách user id
                for (int i = 0; i < m.Count; i++)
                {
                    mangUserID[i] = int.Parse(m[i].Value.Substring(0, m[i].Value.Length - 0));
                    string abc = m[i].Value.Substring(0, m[i].Value.Length - 1);
                    string abc1 = m[i].Value.Substring(0, m[i].Value.Length - 0);
                }
                //lấy danh sách user name
                for (int i = 0; i < m.Count; i++)
                {
                    mangUserName[i] = songService.SreachUserName(mangUserID[i]);
                    string abc = songService.SreachUserName(mangUserID[i]);
                }
                ViewData["mangUserID"] = mangUserID;
                ViewData["mangUserName"] = mangUserName;
                if (CurrentUser == null)
                    return false;
                foreach (var item in mangUserID)
                {
                    if (CurrentUser.Id == item)
                    {
                        return false;
                    }
                }
            }
            model.Thanks += ">" + CurrentUser.Id;
            songService.UpdateThanks(model);
            return true;
        }
        [HttpGet]
        public ActionResult CreateComment(int QAID)
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult CreateComment(QAComment Qacom, int QAID)
        {
            if (CurrentUser != null)
            {
                if (Qacom.CommentContent != null)
                {
                    QAComment qacomment = new QAComment();
                    qacomment.QuestionId = QAID;
                    qacomment.CommentContent = Qacom.CommentContent;
                    qacomment.PostedDate = DateTime.Now;
                    //qacomment.Owners = CurrentUser.Id;
                    qacomment.User = userService.Get(CurrentUser.Id);
                    //TO DO here
                    songService.CreateComany(qacomment);

                    var message = new InternalMessage()
                    {
                        MailStatus = false,
                        MessageContent = "Your QA is answered by " + User.Identity.Name
                        + ". You can visit your QA by URL : http://localhost:8748/QA/Details/" + QAID
                        + Environment.NewLine
                        + "Best regards,"
                        + Environment.NewLine
                        + "Admin",
                        Reciever = songService.FindQaByID(QAID).User.Id,
                        Owners = 1,//userService.FindUserById("admin").UserId,
                        PostedDate = DateTime.Now
                    };

                    //TODO: to be integrated the sending email service
                    //songService.SendMail(message);

                }
            }
            return PartialView();
        }

        //
        // GET: /QA/Create
        public ActionResult Create()
        {

            return View();
        }

        //save
        // POST: /QA/Create

        [HttpPost]
        public ActionResult Create(QAModels Qa)
        {
            try
            {

                QA QA = new QA();
                QA.QuestionTitle = Qa.QuestionTitle;
                QA.QuestionCatagory = Qa.QuestionCatagory;
                QA.QuestionContent = Qa.QuestionContent;
                QA.PostedDate = DateTime.Now;
                QA.IsLock = false;
                QA.SumAnsert = 0;
                QA.SumView = 0;
                QA.IsNew = true;
                //TO DO here
                QA.User = userService.Get(CurrentUser.Id);
                songService.CreateQA(QA);
                return View();
            }
            catch
            {
                return View();
            }
        }
    }
}
